package com.suke.czx.modules.contract.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.ContractSaveGroup;
import com.suke.czx.common.validate.ContractUpdateGroup;
import com.suke.czx.modules.contract.entity.Contract;
import com.suke.czx.modules.contract.entity.ContractSearchBo;
import com.suke.czx.modules.contract.service.ContractService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 合同
 * 代码描述: 合同控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/07 21:04:04
 */
@Validated
@RestController
@RequestMapping("/api/contract")
@AllArgsConstructor
@Api(value = "ContractController", tags = "合同管理")
public class ContractController extends AbstractController {

    private final ContractService contractService;

    /**
     * 分页查询合同列表
     * @param contractSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:contract:list')")
    public R list(ContractSearchBo contractSearchBo) {
        if (contractSearchBo.getPage() == null || contractSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询信息列表
        return contractService.pageContractList(contractSearchBo);
    }

    /**
     * 获取合同详情
     * @param contractId 合同id
     * @return VO
     */
    @RequestMapping(value = "/info/{contractId}", method = RequestMethod.GET)
    @PreAuthorize("hasRole('but:contract:info')")
    public R info(@PathVariable("contractId") Long contractId) {
        Contract contract = contractService.getById(contractId);
        return R.ok().setData(contract);
    }

    /**
     * 保存合同
     * @param contract 合同
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存合同")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:contract:save')")
    public R save(@Validated(ContractSaveGroup.class) Contract contract, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        contract.setCreator(getUserId());
        contract.setCreateTime(new Date());
        contractService.save(contract);
        return R.ok();
    }

    /**
     * 更新合同
     * @param contract 合同
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新合同")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:contract:update')")
    public R update(@Validated(ContractUpdateGroup.class) Contract contract, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        contract.setUpdater(getUserId());
        contract.setUpdateTime(new Date());
        return contractService.updateContract(contract);
    }

    /**
     * 删除合同
     * @param contractId 合同编码
     * @return VO
     */
    @SysLog("删除合同")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:contract:delete')")
    public R delete(@NotNull(message = "合同编码不能为空") Long contractId) {
        return contractService.updateDelContract(contractId, getUserId());
    }

    /**
     * 查询列表
     * @return VO
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:contract:select')")
    public R select(ContractSearchBo contractSearchBo) {
        return contractService.selectContractList(contractSearchBo);
    }

}